import sys

from win32com.client import Dispatch
import win32com
from  TimerWindowsShow import TimerWindowShow
from PyQt5.QtCore import pyqtSignal


class EventListener:
    def __init__(self):
        self.callbacks1 = []
        self.callbacks2 = []
        self.callbacks3 = []
    def register_callback1(self, callback1):
        self.callbacks1.append(callback1)

    def register_callback2(self, callback2):
        self.callbacks2.append(callback2)

    def register_callback3(self, callback3):
        self.callbacks3.append(callback3)

    def trigger_event1(self):
        print('callb1',self.callbacks1)
        for callback1 in self.callbacks1:
            callback1()

    def trigger_event2(self):
        print('callb2',self.callbacks2)
        for callback2 in self.callbacks2:
            callback2()
    def trigger_event3(self):
        print('callb3',self.callbacks3)
        for callback3 in self.callbacks3:
            callback3()

class ShowPPT():
    def __init__(self, path,total_seconds,ShowData,getdatalists=None,TheTimerId='NULL',lcdsetStyleSheet=None):
        super().__init__()
        self.path = path
        self.show=Show(total_seconds=total_seconds,ShowData=ShowData,getdatalists=getdatalists,TheTimerId=TheTimerId,lcdsetStyleSheet=lcdsetStyleSheet)
        self.show.sub_window.subsubsub_window_signal.connect(self.show_main_window)
        self.initUI()

    def show_main_window(self):
        # 发出信号，通知主窗体显示
        print('get2')
        self.show.sub_window.subsubsub_window_signal.emit()

    def initUI(self):
        # 初始化PowerPoint程序
        self.ppt = Dispatch("PowerPoint.Application")

        if self.ppt ==None:
            self.ppt = Dispatch("kwps.Application")
        # event_handler = EventHandler(self.total_seconds, self.ShowData)
        HandEvent = win32com.client.WithEvents(self.ppt,self.show.EventHandler)
        if self.ppt is not None:
            # 打开指定的PPT文件
            self.ppt.Visible = True  # 设置 PowerPoint 窗口可见
            self.ppt.DisplayAlerts = 0  # 设置不显示警告和确认对话框
            presentation=self.ppt.Presentations.Open(self.path)  # 打开PPT文件
            presentation.SlideShowSettings.Run()

        else:
            QMessageBox.warning(self, "Error", "PowerPoint 未安装")
            self.close()


class Show:
    # # 创建事件监听器
    event_listener = EventListener()
    def __init__(self,total_seconds,ShowData,getdatalists=None,TheTimerId='NULL',lcdsetStyleSheet=None):
        self.sub_windows = []  # 存储子窗口实例的列表
        # # 创建事件监听器
        # 传递事件监听器给EventHandler
        self.event_handler = self.EventHandler()
        print('实例化TimerWind')
        self.sub_window = TimerWindowShow(total_seconds=total_seconds, ShowData=ShowData,getdatalists=getdatalists, mode='OtherMode',event_listener=Show.event_listener,TheTimerId=TheTimerId,lcdsetStyleSheet=lcdsetStyleSheet)
        self.sub_window.subsubsub_window_signal.connect(self.show_main_window)
        print('实例化完成')
        self.sub_window.register_for_event()

        # 连接子窗口的信号到槽，确保在主线程中执行
        self.sub_window.showSignal.connect(self.showSubWindow)

        self.sub_windows.append(self.sub_window)

    def show_main_window(self):
        # 发出信号，通知主窗体显示
        print('get3')
        self.sub_window.subsubsub_window_signal.emit()

    def showSubWindow(self, sub_window):
        sub_window.ui.show()

    class EventHandler:
        def OnPresentationOpen(self, Pres):
            self.totalPages=Pres.Slides.Count
            print(self.totalPages)
            print("Presentation Opened:", Pres.Name)

        def OnPresentationClose(self, Presentation):
            Show.event_listener.trigger_event3()
            print("Presentation Closed:", Presentation.Name)

        def OnSlideShowBegin(self, Wn):
            Show.event_listener.trigger_event1()
            print("Presentation Run:", Wn)

        def OnSlideShowNextSlide(self, Wn):
            try:
                # print(Wn.View.Slide.SlideIndex,self.totalPages)
                print("now: Slide:", Wn.View.Slide.SlideIndex)
                if Wn.View.Slide.SlideIndex == self.totalPages:
                    self.OnLastSlideReached()

            except Exception as e:
                print('OnSlideShowNextSlideError:',{e})

        def OnSlideShowPreviousSlide(self, Wn):
            print("Pre Slide:", Wn.View.Slide.SlideIndex)

        def OnLastSlideReached(self):
            Show.event_listener.trigger_event2()
            print("Last Slide Reached!")

        def OnSlideShowEnd(self, Wn):
            Show.event_listener.trigger_event2()
            print("Presentation End:", Wn)





# if __name__ == "__main__":
#     path_to_ppt = r"D:\Manual\RUANJIAN\qq\qqdocument\861912251\FileRecv\21通2无言小组.pptx"
#     showPPT = ShowPPT(path_to_ppt)
#     pythoncom.PumpMessages()




